package com.zretc.support.controller;

import com.zretc.support.entity.Answer;
import com.zretc.support.entity.Collection;
import com.zretc.support.service.CollectionService;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/coll")
public class CollectionController {
    @Autowired
    CollectionService collectionService;

    AjaxResponse ajaxResponse = new AjaxResponse();

    PageData<Collection> pageData = new PageData<>();

    List<String> arraylist = null;

    /**
     * 添加收藏
     *
     * @param collection
     * @return
     */
    @PostMapping(value = "addcoll", produces = "application/json;charset=utf-8")
    AjaxResponse addColl(@RequestBody Collection collection) {
        collectionService.addCollection(collection);

        ajaxResponse.setCode(200);
        ajaxResponse.setMsg("添加成功");
        return ajaxResponse;
    }

    /**
     * 删除收藏
     *
     * @param collectionId 收藏编号
     * @return
     */
    @PostMapping(value = "delcoll", produces = "application/json;charset=utf-8")
    AjaxResponse delColl(int collectionId) {
        collectionService.cancelCollection(collectionId);

        ajaxResponse.setCode(200);
        ajaxResponse.setMsg("删除成功");
        return ajaxResponse;
    }

    /**
     * 分页通过id获取收藏信息
     *
     * @param userid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getcollbyid")
    @ResponseBody
    public PageData<Collection> getlike(@RequestParam(value = "userid", defaultValue = "") String userid,
                                        @RequestParam(value = "page", defaultValue = "1") int page,
                                        @RequestParam(value = "limit", defaultValue = "10") int limit) {

        Page<Collection> pageinfo = collectionService.findCollByUserId(userid, page, limit);

        arraylist = new ArrayList<>();
        //遍历
        for (int i=0;i<pageinfo.getTotalElements();i++){
            arraylist.add(pageinfo.getContent().get(i).getAnswer().getTopicId());
        }

        //每页的集合数据
        pageData.setRecords(pageinfo.getContent());
        //当前页
        pageData.setCurrent(pageinfo.getNumber() + 1);
        //总记录数
        pageData.setTotal((int) pageinfo.getTotalElements());
        //每页的记录数
        pageData.setSize(pageinfo.getSize());
        //
        pageData.setCode(200);
        //
        pageData.setMsg("查询成功");

        return pageData;
    }

    /**
     * 根据试题的id数组查询判断题这些试题
     *
     * @return
     */
    @GetMapping(value = "getcollinid")
    @ResponseBody
    public Object getCollInId() {

        return collectionService.getByTopicInId(arraylist).get(0);
    }

    /**
     * 根据试题的id数组查询单选题这些试题
     *
     * @return
     */
    @GetMapping(value = "getcollinid2")
    @ResponseBody
    public Object getCollInId2() {

        return collectionService.getByTopicInId(arraylist).get(1);
    }

    /**
     * 根据试题编号查询考生的answer
     * @param topicId
     * @return
     */
    @RequestMapping(value = "getAnswerById/{topicId}/{userId}",method = RequestMethod.GET)
    public Answer getTopicByTopicId(@PathVariable("topicId") String topicId, @PathVariable("userId")String userId){

        return collectionService.getTopicByTopicId(topicId,userId);
    }



}
